package lp.xian.production.service.impl;

import java.util.Date;
import java.util.List;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import lp.xian.production.domain.FinishedProduct;
import lp.xian.production.domain.ProductInventory;
import lp.xian.production.mapper.FinishedProductMapper;

import lp.xian.production.mapper.ProductInventoryMapper;
import lp.xian.production.vo.ProductionInventoryVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import lp.xian.production.service.IProductInventoryService;

/**
 * 生产损耗Service业务层处理
 *
 * @author ruoyi
 * @date 2023-07-05
 */
@Service
public class ProductInventoryServiceImpl implements IProductInventoryService
{
    @Autowired
    private ProductInventoryMapper productInventoryMapper;
    @Autowired
    private FinishedProductMapper finishedProductMapper;

    /**
     * 查询生产损耗
     *
     * @param productionInventoryId 生产损耗主键
     * @return 生产损耗
     */
    @Override
    public ProductInventory selectProductInventoryByProductionInventoryId(Integer productionInventoryId)
    {
        return productInventoryMapper.selectProductInventoryByProductionInventoryId(productionInventoryId);
    }

    /**
     * 查询生产损耗列表
     *
     * @param productInventory 生产损耗
     * @return 生产损耗
     */
    @Override
    public List<ProductInventory> selectProductInventoryList(ProductInventory productInventory)
    {
        return productInventoryMapper.selectProductInventoryList(productInventory);
    }

    /**
     * 新增生产损耗
     *
     * @param productInventory 生产损耗
     * @return 结果
     */
    @Override
    public int insertProductInventory(ProductInventory productInventory)
    {
        productInventory.setCreateTime(DateUtils.getNowDate());
        return productInventoryMapper.insertProductInventory(productInventory);
    }



    /**
     * 修改生产损耗
     *
     * @param productionInventoryVo 生产损耗
     * @return 结果
     */
    @Override
    public int updateProductInventory(ProductionInventoryVo productionInventoryVo) {
        ProductInventory productInventory=new ProductInventory();
        productInventory.setProductionInventoryUnit(productionInventoryVo.getFinishedProductUnit());
        productInventory.setProductionInventoryId(productionInventoryVo.getProductionInventoryId());
        productInventory.setFinishedProductName(productionInventoryVo.getFinishedProductName());
        productInventory.setFinishedProductNum(productionInventoryVo.getFinishedProductNum());
        productInventory.setFinishedProductUnit(productionInventoryVo.getFinishedProductUnit());
        productInventory.setLossNum(productionInventoryVo.getLossNum());
        productInventory.setLossName(productionInventoryVo.getLossName());
        productInventory.setLossUnit(productionInventoryVo.getLossUnit());
        productInventory.setFristDuty(productionInventoryVo.getFristDuty());
        productInventory.setFristDutyPhone(productionInventoryVo.getFristDutyPhone());
        productInventory.setOffTime(productionInventoryVo.getOffTime());
        productInventory.setOfficeTime(productionInventoryVo.getOfficeTime());
        productInventory.setUpdateTime(new Date());
        productInventory.setGradeClass(productionInventoryVo.getGradeClass());
        productInventory.setOfficeNum(productionInventoryVo.getOfficeNum());
        return  productInventoryMapper.updateProductInventory(productInventory);
    }

    /**
     * 批量删除生产损耗
     *
     * @param productionInventoryIds 需要删除的生产损耗主键
     * @return 结果
     */
    @Override
    public int deleteProductInventoryByProductionInventoryIds(Integer[] productionInventoryIds)
    {
        return productInventoryMapper.deleteProductInventoryByProductionInventoryIds(productionInventoryIds);
    }

    /**
     * 删除生产损耗信息
     *
     * @param productionInventoryId 生产损耗主键
     * @return 结果
     */
    @Override
    public int deleteProductInventoryByProductionInventoryId(Integer productionInventoryId)
    {
        return productInventoryMapper.deleteProductInventoryByProductionInventoryId(productionInventoryId);
    }

    @Override
    public int auditProductInvenory(ProductionInventoryVo productionInventoryVo) {
        ProductInventory productInventory=new ProductInventory();
        productInventory.setProductionInventoryId(productionInventoryVo.getProductionInventoryId());
        productInventory.setAuditStatus(productionInventoryVo.getAuditStatus());
        productInventory.setAuditOpinion(productionInventoryVo.getAuditOpinion());
        productInventory.setAuditPerson(SecurityUtils.getUsername());
        productInventory.setAuditTime(new Date());
        return productInventoryMapper.updateProductInventory(productInventory);
    }

    @Override
    public int approverProductInvenory(ProductionInventoryVo productionInventoryVo) {
        //(1)审批提交数据库
        ProductInventory productInventory=new ProductInventory();
        productInventory.setProductionInventoryId(productionInventoryVo.getProductionInventoryId());
        productInventory.setAuditStatus(productionInventoryVo.getAuditStatus());
        productInventory.setApproverOpinion(productionInventoryVo.getApproverOpinion());
        productInventory.setApproverPerson(SecurityUtils.getUsername());
        productInventory.setApproverTime(new Date());
        int num=productInventoryMapper.updateProductInventory(productInventory);
        //(2)需要查询出数据 将相关值赋给成品值
        if (num==1){
           ProductInventory productInventory1=productInventoryMapper.selectProductInventoryByProductionInventoryId(productionInventoryVo.getProductionInventoryId());
           if (productInventory1.getAuditStatus().intValue()==2){
               FinishedProduct finishedProduct=new FinishedProduct();
               finishedProduct.setFinishProductName(productInventory1.getFinishedProductName());
               finishedProduct.setFinishProductNum(productInventory1.getFinishedProductNum());
               finishedProduct.setFinishProductUnit(productInventory1.getFinishedProductUnit());
               finishedProduct.setFristDuty(productInventory1.getFristDuty());
               finishedProduct.setFristDutyPhone(productInventory1.getFristDutyPhone());
               finishedProduct.setGradeClass(productInventory1.getGradeClass());
               finishedProduct.setOfficeTime(productInventory1.getOfficeTime());
               finishedProduct.setOffTime(productInventory1.getOffTime());
               finishedProduct.setProductPrice(productInventory1.getProductionInventoryPrice());
               finishedProduct.setOfficeNum(productInventory1.getOfficeNum());
               finishedProduct.setCreateTime(new Date());
               finishedProduct.setCreateBy("系统生成");
              return finishedProductMapper.insertFinishedProduct(finishedProduct);

           }
        }
        return 1;
    }
}
